// $ANTLR 3.4 src/main/resources/ModelRef.g 2012-02-18 23:27:48
package samples;

import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
import java.util.Stack;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;

@SuppressWarnings({"all", "warnings", "unchecked"})
public class ModelRef extends TreeFilter {
    public static final String[] tokenNames = new String[] {
        "<invalid>", "<EOR>", "<DOWN>", "<UP>", "CLSS_DECL", "COMMENT", "FIELD_DECL", "IDENTIFIER", "IDXF_DECL", "INDX_DECL", "ITFA_DECL", "LINE_COMMENT", "MEMBERS", "METHOD_DECL", "MODF_DECL", "REL_DECL", "TYPE_DECL", "UNIT", "WS", "'\"'", "'('", "')'", "','", "'.'", "';'", "'='", "'[]'", "'abstract'", "'class'", "'extends'", "'implements'", "'import'", "'interface'", "'null'", "'package'", "'public'", "'static'", "'{'", "'}'"
    };

    public static final int EOF=-1;
    public static final int T__19=19;
    public static final int T__20=20;
    public static final int T__21=21;
    public static final int T__22=22;
    public static final int T__23=23;
    public static final int T__24=24;
    public static final int T__25=25;
    public static final int T__26=26;
    public static final int T__27=27;
    public static final int T__28=28;
    public static final int T__29=29;
    public static final int T__30=30;
    public static final int T__31=31;
    public static final int T__32=32;
    public static final int T__33=33;
    public static final int T__34=34;
    public static final int T__35=35;
    public static final int T__36=36;
    public static final int T__37=37;
    public static final int T__38=38;
    public static final int CLSS_DECL=4;
    public static final int COMMENT=5;
    public static final int FIELD_DECL=6;
    public static final int IDENTIFIER=7;
    public static final int IDXF_DECL=8;
    public static final int INDX_DECL=9;
    public static final int ITFA_DECL=10;
    public static final int LINE_COMMENT=11;
    public static final int MEMBERS=12;
    public static final int METHOD_DECL=13;
    public static final int MODF_DECL=14;
    public static final int REL_DECL=15;
    public static final int TYPE_DECL=16;
    public static final int UNIT=17;
    public static final int WS=18;

    // delegates
    public TreeFilter[] getDelegates() {
        return new TreeFilter[] {};
    }

    // delegators


    public ModelRef(TreeNodeStream input) {
        this(input, new RecognizerSharedState());
    }
    public ModelRef(TreeNodeStream input, RecognizerSharedState state) {
        super(input, state);
    }

    public String[] getTokenNames() { return ModelRef.tokenNames; }
    public String getGrammarFileName() { return "src/main/resources/ModelRef.g"; }



    // $ANTLR start "topdown"
    // src/main/resources/ModelRef.g:14:1: topdown : ( enterMethod | enterClass | enterInterface | fieldDeclaration );
    public final void topdown() throws RecognitionException {
        try {
            // src/main/resources/ModelRef.g:15:5: ( enterMethod | enterClass | enterInterface | fieldDeclaration )
            int alt1=4;
            switch ( input.LA(1) ) {
            case METHOD_DECL:
                {
                alt1=1;
                }
                break;
            case 28:
                {
                alt1=2;
                }
                break;
            case 32:
                {
                alt1=3;
                }
                break;
            case FIELD_DECL:
                {
                alt1=4;
                }
                break;
            default:
                if (state.backtracking>0) {state.failed=true; return ;}
                NoViableAltException nvae =
                    new NoViableAltException("", 1, 0, input);

                throw nvae;

            }

            switch (alt1) {
                case 1 :
                    // src/main/resources/ModelRef.g:15:9: enterMethod
                    {
                    pushFollow(FOLLOW_enterMethod_in_topdown58);
                    enterMethod();

                    state._fsp--;
                    if (state.failed) return ;

                    }
                    break;
                case 2 :
                    // src/main/resources/ModelRef.g:16:9: enterClass
                    {
                    pushFollow(FOLLOW_enterClass_in_topdown68);
                    enterClass();

                    state._fsp--;
                    if (state.failed) return ;

                    }
                    break;
                case 3 :
                    // src/main/resources/ModelRef.g:17:7: enterInterface
                    {
                    pushFollow(FOLLOW_enterInterface_in_topdown76);
                    enterInterface();

                    state._fsp--;
                    if (state.failed) return ;

                    }
                    break;
                case 4 :
                    // src/main/resources/ModelRef.g:18:9: fieldDeclaration
                    {
                    pushFollow(FOLLOW_fieldDeclaration_in_topdown86);
                    fieldDeclaration();

                    state._fsp--;
                    if (state.failed) return ;

                    }
                    break;

            }
        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }

        finally {
        	// do for sure before leaving
        }
        return ;
    }
    // $ANTLR end "topdown"



    // $ANTLR start "enterInterface"
    // src/main/resources/ModelRef.g:21:1: enterInterface : ^( 'interface' name= IDENTIFIER ( ^( 'extends' sup= IDENTIFIER ) )? . ) ;
    public final void enterInterface() throws RecognitionException {
        ModelAST name=null;
        ModelAST sup=null;

        try {
            // src/main/resources/ModelRef.g:22:5: ( ^( 'interface' name= IDENTIFIER ( ^( 'extends' sup= IDENTIFIER ) )? . ) )
            // src/main/resources/ModelRef.g:22:9: ^( 'interface' name= IDENTIFIER ( ^( 'extends' sup= IDENTIFIER ) )? . )
            {
            match(input,32,FOLLOW_32_in_enterInterface106); if (state.failed) return ;

            match(input, Token.DOWN, null); if (state.failed) return ;
            name=(ModelAST)match(input,IDENTIFIER,FOLLOW_IDENTIFIER_in_enterInterface110); if (state.failed) return ;

            // src/main/resources/ModelRef.g:22:39: ( ^( 'extends' sup= IDENTIFIER ) )?
            int alt2=2;
            int LA2_0 = input.LA(1);

            if ( (LA2_0==29) ) {
                int LA2_1 = input.LA(2);

                if ( (LA2_1==DOWN) ) {
                    int LA2_3 = input.LA(3);

                    if ( (LA2_3==IDENTIFIER) ) {
                        int LA2_4 = input.LA(4);

                        if ( (LA2_4==UP) ) {
                            int LA2_5 = input.LA(5);

                            if ( ((LA2_5 >= CLSS_DECL && LA2_5 <= 38)) ) {
                                alt2=1;
                            }
                        }
                    }
                }
            }
            switch (alt2) {
                case 1 :
                    // src/main/resources/ModelRef.g:22:40: ^( 'extends' sup= IDENTIFIER )
                    {
                    match(input,29,FOLLOW_29_in_enterInterface114); if (state.failed) return ;

                    match(input, Token.DOWN, null); if (state.failed) return ;
                    sup=(ModelAST)match(input,IDENTIFIER,FOLLOW_IDENTIFIER_in_enterInterface118); if (state.failed) return ;

                    match(input, Token.UP, null); if (state.failed) return ;


                    }
                    break;

            }


            matchAny(input); if (state.failed) return ;

            match(input, Token.UP, null); if (state.failed) return ;


            if ( state.backtracking==1 ) {
                    if ( sup!=null ) {
                        // look up superclass (if any)
                        sup.symbol = sup.scope.resolve((sup!=null?sup.getText():null));
                        ((ClassSymbol)name.symbol).superClass =
                            (ClassSymbol)sup.symbol;                // set superclass
                        System.out.println("line "+name.getLine()+": set "+(name!=null?name.getText():null)+
                            " super to "+((ClassSymbol)name.symbol).superClass);
                    }
                    else {
                        System.out.println("line "+name.getLine()+": set "+name.symbol);
                    }
                    }

            }

        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }

        finally {
        	// do for sure before leaving
        }
        return ;
    }
    // $ANTLR end "enterInterface"



    // $ANTLR start "enterClass"
    // src/main/resources/ModelRef.g:38:1: enterClass : ^( 'class' name= IDENTIFIER ( ^( 'implements' sup= IDENTIFIER ) )? . ) ;
    public final void enterClass() throws RecognitionException {
        ModelAST name=null;
        ModelAST sup=null;

        try {
            // src/main/resources/ModelRef.g:39:5: ( ^( 'class' name= IDENTIFIER ( ^( 'implements' sup= IDENTIFIER ) )? . ) )
            // src/main/resources/ModelRef.g:39:9: ^( 'class' name= IDENTIFIER ( ^( 'implements' sup= IDENTIFIER ) )? . )
            {
            match(input,28,FOLLOW_28_in_enterClass154); if (state.failed) return ;

            match(input, Token.DOWN, null); if (state.failed) return ;
            name=(ModelAST)match(input,IDENTIFIER,FOLLOW_IDENTIFIER_in_enterClass158); if (state.failed) return ;

            // src/main/resources/ModelRef.g:39:35: ( ^( 'implements' sup= IDENTIFIER ) )?
            int alt3=2;
            int LA3_0 = input.LA(1);

            if ( (LA3_0==30) ) {
                int LA3_1 = input.LA(2);

                if ( (LA3_1==DOWN) ) {
                    int LA3_3 = input.LA(3);

                    if ( (LA3_3==IDENTIFIER) ) {
                        int LA3_4 = input.LA(4);

                        if ( (LA3_4==UP) ) {
                            int LA3_5 = input.LA(5);

                            if ( ((LA3_5 >= CLSS_DECL && LA3_5 <= 38)) ) {
                                alt3=1;
                            }
                        }
                    }
                }
            }
            switch (alt3) {
                case 1 :
                    // src/main/resources/ModelRef.g:39:36: ^( 'implements' sup= IDENTIFIER )
                    {
                    match(input,30,FOLLOW_30_in_enterClass162); if (state.failed) return ;

                    match(input, Token.DOWN, null); if (state.failed) return ;
                    sup=(ModelAST)match(input,IDENTIFIER,FOLLOW_IDENTIFIER_in_enterClass166); if (state.failed) return ;

                    match(input, Token.UP, null); if (state.failed) return ;


                    }
                    break;

            }


            matchAny(input); if (state.failed) return ;

            match(input, Token.UP, null); if (state.failed) return ;


            if ( state.backtracking==1 ) {
                    if ( sup!=null ) {
                        // look up superclass (if any)
                        sup.symbol = sup.scope.resolve((sup!=null?sup.getText():null));
                        ((ClassSymbol)name.symbol).superClass =
                            (ClassSymbol)sup.symbol;                // set superclass
                        System.out.println("line "+name.getLine()+": set "+(name!=null?name.getText():null)+
                            " super to "+((ClassSymbol)name.symbol).superClass);
                    }
                    else {
                        System.out.println("line "+name.getLine()+": set "+name.symbol);
                    }
                    }

            }

        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }

        finally {
        	// do for sure before leaving
        }
        return ;
    }
    // $ANTLR end "enterClass"



    // $ANTLR start "enterMethod"
    // src/main/resources/ModelRef.g:55:1: enterMethod : ^( METHOD_DECL type ID= IDENTIFIER ) ;
    public final void enterMethod() throws RecognitionException {
        ModelAST ID=null;
        Type type1 =null;


        try {
            // src/main/resources/ModelRef.g:56:5: ( ^( METHOD_DECL type ID= IDENTIFIER ) )
            // src/main/resources/ModelRef.g:56:9: ^( METHOD_DECL type ID= IDENTIFIER )
            {
            match(input,METHOD_DECL,FOLLOW_METHOD_DECL_in_enterMethod202); if (state.failed) return ;

            match(input, Token.DOWN, null); if (state.failed) return ;
            pushFollow(FOLLOW_type_in_enterMethod204);
            type1=type();

            state._fsp--;
            if (state.failed) return ;

            ID=(ModelAST)match(input,IDENTIFIER,FOLLOW_IDENTIFIER_in_enterMethod208); if (state.failed) return ;

            match(input, Token.UP, null); if (state.failed) return ;


            if ( state.backtracking==1 ) {
                    ID.symbol.type = (Type) type1; // set return type of method
                    System.out.println("line "+ID.getLine()+": set method type "+type1);
                    }

            }

        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }

        finally {
        	// do for sure before leaving
        }
        return ;
    }
    // $ANTLR end "enterMethod"



    // $ANTLR start "fieldDeclaration"
    // src/main/resources/ModelRef.g:64:1: fieldDeclaration : ^( FIELD_DECL type ID= IDENTIFIER ) ;
    public final void fieldDeclaration() throws RecognitionException {
        ModelAST ID=null;
        Type type2 =null;


        try {
            // src/main/resources/ModelRef.g:65:5: ( ^( FIELD_DECL type ID= IDENTIFIER ) )
            // src/main/resources/ModelRef.g:65:9: ^( FIELD_DECL type ID= IDENTIFIER )
            {
            match(input,FIELD_DECL,FOLLOW_FIELD_DECL_in_fieldDeclaration241); if (state.failed) return ;

            match(input, Token.DOWN, null); if (state.failed) return ;
            pushFollow(FOLLOW_type_in_fieldDeclaration244);
            type2=type();

            state._fsp--;
            if (state.failed) return ;

            ID=(ModelAST)match(input,IDENTIFIER,FOLLOW_IDENTIFIER_in_fieldDeclaration248); if (state.failed) return ;

            match(input, Token.UP, null); if (state.failed) return ;


            if ( state.backtracking==1 ) {
                    ID.symbol.type = (Type) type2; // set return type of variable
                    System.out.println("line "+ID.getLine()+": set VarType "+ID.symbol);
                    }

            }

        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }

        finally {
        	// do for sure before leaving
        }
        return ;
    }
    // $ANTLR end "fieldDeclaration"



    // $ANTLR start "type"
    // src/main/resources/ModelRef.g:72:1: type returns [Type tsym] : ^( ( REL_DECL | TYPE_DECL ) ID= IDENTIFIER ) ;
    public final Type type() throws RecognitionException {
        Type tsym = null;


        ModelAST ID=null;

        try {
            // src/main/resources/ModelRef.g:73:5: ( ^( ( REL_DECL | TYPE_DECL ) ID= IDENTIFIER ) )
            // src/main/resources/ModelRef.g:73:9: ^( ( REL_DECL | TYPE_DECL ) ID= IDENTIFIER )
            {
            if ( (input.LA(1) >= REL_DECL && input.LA(1) <= TYPE_DECL) ) {
                input.consume();
                state.errorRecovery=false;
                state.failed=false;
            }
            else {
                if (state.backtracking>0) {state.failed=true; return tsym;}
                MismatchedSetException mse = new MismatchedSetException(null,input);
                throw mse;
            }


            match(input, Token.DOWN, null); if (state.failed) return tsym;
            ID=(ModelAST)match(input,IDENTIFIER,FOLLOW_IDENTIFIER_in_type291); if (state.failed) return tsym;

            match(input, Token.UP, null); if (state.failed) return tsym;


            if ( state.backtracking==1 ) {
            	    // get scope from AST; use to resolve type name and save it in AST
            	    ID.symbol = ID.scope.resolve((ID!=null?ID.getText():null));
            	    tsym = (Type)ID.symbol; // return Type from this rule
            		}

            }

        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }

        finally {
        	// do for sure before leaving
        }
        return tsym;
    }
    // $ANTLR end "type"

    // Delegated rules


 

    public static final BitSet FOLLOW_enterMethod_in_topdown58 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_enterClass_in_topdown68 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_enterInterface_in_topdown76 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_fieldDeclaration_in_topdown86 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_32_in_enterInterface106 = new BitSet(new long[]{0x0000000000000004L});
    public static final BitSet FOLLOW_IDENTIFIER_in_enterInterface110 = new BitSet(new long[]{0x0000007FFFFFFFF0L});
    public static final BitSet FOLLOW_29_in_enterInterface114 = new BitSet(new long[]{0x0000000000000004L});
    public static final BitSet FOLLOW_IDENTIFIER_in_enterInterface118 = new BitSet(new long[]{0x0000000000000008L});
    public static final BitSet FOLLOW_28_in_enterClass154 = new BitSet(new long[]{0x0000000000000004L});
    public static final BitSet FOLLOW_IDENTIFIER_in_enterClass158 = new BitSet(new long[]{0x0000007FFFFFFFF0L});
    public static final BitSet FOLLOW_30_in_enterClass162 = new BitSet(new long[]{0x0000000000000004L});
    public static final BitSet FOLLOW_IDENTIFIER_in_enterClass166 = new BitSet(new long[]{0x0000000000000008L});
    public static final BitSet FOLLOW_METHOD_DECL_in_enterMethod202 = new BitSet(new long[]{0x0000000000000004L});
    public static final BitSet FOLLOW_type_in_enterMethod204 = new BitSet(new long[]{0x0000000000000080L});
    public static final BitSet FOLLOW_IDENTIFIER_in_enterMethod208 = new BitSet(new long[]{0x0000000000000008L});
    public static final BitSet FOLLOW_FIELD_DECL_in_fieldDeclaration241 = new BitSet(new long[]{0x0000000000000004L});
    public static final BitSet FOLLOW_type_in_fieldDeclaration244 = new BitSet(new long[]{0x0000000000000080L});
    public static final BitSet FOLLOW_IDENTIFIER_in_fieldDeclaration248 = new BitSet(new long[]{0x0000000000000008L});
    public static final BitSet FOLLOW_set_in_type283 = new BitSet(new long[]{0x0000000000000004L});
    public static final BitSet FOLLOW_IDENTIFIER_in_type291 = new BitSet(new long[]{0x0000000000000008L});

}